package com.sywb.chuangyebao.contract;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;

import org.bining.footstone.adapter.BaseRecyclerAdapter;
import org.bining.footstone.adapter.BaseRecyclerDivider;
import org.bining.footstone.adapter.listener.OnItemChildClickListener;
import org.bining.footstone.adapter.listener.OnItemClickListener;
import org.bining.footstone.adapter.listener.OnItemLongClickListener;
import org.bining.footstone.adapter.listener.OnLoadMoreListener;
import org.bining.footstone.log.Logger;
import org.bining.footstone.mvp.IView;
import org.bining.footstone.presenter.BasePresenter;

public interface BaseRecyclerContract {

    interface View extends IView {

        /**
         * 获取 RecyclerView
         */
        RecyclerView getRecyclerView();

        /**
         * 设置获取数据完毕时的界面展示
         */

        void setNoData(boolean isNoData);
    }

    abstract class Presenter<T extends View> extends BasePresenter<T> {

        protected RecyclerView commonRecycler;

        protected BaseRecyclerDivider recyclerDivider;
        protected BaseRecyclerAdapter baseRecyclerAdapter;

        protected boolean isLoadMore;

        @Override
        public void onStart() {
            commonRecycler = mView.getRecyclerView();
            if (commonRecycler != null) {
                switch (useRecyclerLayoutManager()) {
                    case 0:
                        commonRecycler.setLayoutManager(new LinearLayoutManager(mContext));
                        break;
                    case 1:
                        commonRecycler.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.HORIZONTAL, false));
                        break;
                    case 2:
                        commonRecycler.setLayoutManager(new GridLayoutManager(mActivity, useGridLayoutManagerColumns()));
                        break;
                }
                if (useRecyclerDivider()) {
                    recyclerDivider = new BaseRecyclerDivider();
                    commonRecycler.addItemDecoration(recyclerDivider);
                }
            }
        }

        public void setRecyclerAdapter(BaseRecyclerAdapter recyclerAdapter) {
            setRecyclerAdapter(recyclerAdapter, true);
        }

        public void setRecyclerAdapter(final BaseRecyclerAdapter recyclerAdapter, boolean isLoadMore) {
            if (recyclerAdapter != null && commonRecycler != null) {

                this.isLoadMore = isLoadMore;
                if (isLoadMore) {
                    recyclerAdapter.openLoadMore(R.layout.layout_loading, Constants.PAGESIZE);
                    recyclerAdapter.setLoadMoreEnable(false);
                    recyclerAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
                        @Override
                        public void onLoadMoreClick() {
                            onStartLoadMore();
                        }
                    });
                }

                recyclerAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(android.view.View view, int i) {
                        if (i < (recyclerAdapter.getDataCount() + recyclerAdapter.getHeaderViewsCount())) {
                            onRecyclerItemClick(i);
                        }
                    }
                });

                recyclerAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClick(android.view.View view, int i) {
                        if (i < (recyclerAdapter.getDataCount() + recyclerAdapter.getHeaderViewsCount())) {
                            onRecyclerItemLongClick(view, i);
                        }
                        return false;
                    }
                });

                recyclerAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
                    @Override
                    public void onItemChildClick(android.view.View view, int i) {
                        if (i < (recyclerAdapter.getDataCount() + recyclerAdapter.getHeaderViewsCount())) {
                            onRecyclerItemChildClick(view, i);
                        }
                    }
                });

                commonRecycler.setAdapter(recyclerAdapter);
                baseRecyclerAdapter = recyclerAdapter;
            }
        }

        public void initData() {
            onStartAsync();
        }

        public void onStartRefresh() {
            Logger.e("onStartRefresh");
        }

        public void onEndRefresh() {
            Logger.e("onEndRefresh");
        }

        public void onStartLoadMore() {
            Logger.e("onStartLoadMore");
        }

        public void onEndLoadMore() {
            Logger.e("onEndLoadMore");
            if (baseRecyclerAdapter != null) {
                baseRecyclerAdapter.closeLoadMore();
            }
        }

        public void onRecyclerItemClick(int postion) {
        }

        public void onRecyclerItemLongClick(android.view.View view, int postion) {
        }

        public void onRecyclerItemChildClick(android.view.View view, int postion) {
        }

        public void isNoData() {
            if (baseRecyclerAdapter != null && mView != null) {
                mView.setNoData(baseRecyclerAdapter.getDataCount() == 0);
            }
        }

        public int useRecyclerLayoutManager() {
            return 0;
        }

        public int useGridLayoutManagerColumns() {
            return 4;
        }

        public boolean useRecyclerDivider() {
            return true;
        }

        public void onNetWorkChange(boolean isConnect) {
            if (isConnect) {
                if (isLoadMore) {
                    baseRecyclerAdapter.resectLoadMore();
                }
            } else {
                if (isLoadMore) {
                    baseRecyclerAdapter.closeLoadMore();
                }
                if (baseRecyclerAdapter.getDataCount() == 0) {
                    mView.setNoData(true);
                }
            }
        }
    }
}
